package cn.zzp.org.service.impl;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.basic.util.BaiduAuditUtils;
import cn.zzp.basic.util.EmailUtils;
import cn.zzp.basic.util.FastDfsUtils;
import cn.zzp.org.domain.Employee;
import cn.zzp.org.domain.Shop;
import cn.zzp.org.domain.ShopDto;
import cn.zzp.org.domain.ShopLog;
import cn.zzp.org.mapper.EmployeeMapper;
import cn.zzp.org.mapper.ShopLogMapper;
import cn.zzp.org.mapper.ShopMapper;
import cn.zzp.org.service.EmployeeService;
import cn.zzp.org.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @ClassName EmployeeServiceImpl
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/14 14:16
 */
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private EmailUtils emailUtils;

    @Transactional
    @Override
    public void settlement(Shop shop) {
        // 填写表单内容不能为空
        Employee admin = shop.getAdmin();
        if (
                StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress())

                || StringUtils.isEmpty(admin.getUsername())
                || StringUtils.isEmpty(admin.getPhone())
                || StringUtils.isEmpty(admin.getEmail())
                || StringUtils.isEmpty(admin.getPassword())
                || StringUtils.isEmpty(admin.getComfirmPassword())
        ) {
            // 为空抛个异常给前端
            throw new BusinessException("必填参数不能为空");
        }

        // 判断店铺名称是否合规
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean){
            throw new BusinessException("店铺名称违规！");
        }

        // 判断两次密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次输入的密码不一致，请重新确认");
        }

        // 判断店铺是否已经注册过
        Shop dbShop = shopMapper.queryByMessage(shop);

        if (dbShop == null) { // 新进入店铺
            // 将数据存入数据库


            employeeService.add(admin);
            shop.setAdmin_id(admin.getId());

            shopMapper.add(shop);
            admin.setShop_id(shop.getId());
            employeeService.update(admin);
        }else if ( dbShop.getState() == 4){ // 被驳回重新申请
            // 更新店长信息
            admin.setId(dbShop.getAdmin_id());
            employeeService.update(admin);

            // 更新商店信息，把状态改回待审核
            shop.setId(dbShop.getId());
            shop.setState(1);
            shop.setAdmin_id(admin.getId());
            shopMapper.update(shop);
        }else { // 店铺已经注册过
            throw new BusinessException("该店铺已经注册！！！");
        }



    }

    /**
     * 审核通过操作
     * @param log
     */
    @Transactional
    @Override
    public void pass(ShopLog log) {
        // 驳回先修改shop状态
        Shop dbShop = shopMapper.queryById(log.getShop_id());
        dbShop.setState(4);
        shopMapper.update(dbShop);

        // 再添加审核信息
        log.setState(4);
        log.setAudit_id(1L); // 暂时写个固定值，后面做了登录就能填入了
        shopLogMapper.add(log);

        // 最后发送邮件
        String to = employeeMapper.queryById(dbShop.getAdmin_id()).getEmail();
        String message = "<h1>入驻失败!!!</h1>" +
                "<a href='http://localhost:8081/#/register'>点击重新入驻</a>";
        emailUtils.send(to,"入驻失败邮件",message,null);

    }

    /**
     * 驳回审核
     * @param log
     */
    @Transactional
    @Override
    public void reject(ShopLog log) {
        // 审核通过先修改shop状态
        Shop dbShop = shopMapper.queryById(log.getShop_id());
        dbShop.setState(2);
        shopMapper.update(dbShop);

        // 再添加审核信息
        log.setState(2);
        log.setAudit_id(1L); // 暂时写个固定值，后面做了登录就能填入了
        shopLogMapper.add(log);

        // 最后发送邮件
        String to = employeeMapper.queryById(dbShop.getAdmin_id()).getEmail();
        String message = "<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+dbShop.getLogo()+"' ><a href='http://localhost:8080/shop/active/"+log.getShop_id()+"'>点击该链接激活</a>";
        emailUtils.send(to,"店铺激活邮件",message,null);

    }

    /**
     * 批量插入
     * @param shops
     */
    @Transactional
    @Override
    public void patchAdd(List<Shop> shops) {
        shopMapper.patchAdd(shops);
    }

    /**
     * 查询状态次数
     * @return
     */
    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

    @Transactional
    @Override
    public void delete(Long id){
        // 删除数据库商店信息时先删除商店图片
        Shop shop = shopMapper.queryById(id);
        String logo = shop.getLogo();
        if (!StringUtils.isEmpty(logo)) {
            String pathTmp = logo.substring(1);
            String groupName = pathTmp.substring(0, pathTmp.indexOf("/"));
            String fileName = pathTmp.substring(pathTmp.indexOf("/") + 1);
            // 调用文件删除方法
            FastDfsUtils.delete(groupName,fileName);
        }

        // 删除商店信息，应该将emp表中对应的工作人员删除
        employeeMapper.delByShopId(shop.getId());

        // 删除数据库数据
        shopMapper.delete(id);
    }
}
